#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <UIKit/UIKit.h>
#import <UIKit/UIApplication.h>
#import <EventKit/EventKit.h>
#import <LockInfo/Plugin.h>

#import "framework/FWCommon.h"
#import "framework/FWApplication.h"
#import "framework/FWPreferences.h"

#import "PICommon.h"
#import "PIIntegration.h"
#import "PIContextObject.h"
#import "PIAlarm.h"
#import "PIPlugin.h"

#import "PIEventIntegration.h"
#import "PIEventIntegration202.h"
#import "PIEvent.h"
#import "PIAttendee.h"


@implementation PIEventIntegration202


// ******************************************************************************************
#pragma mark -
#pragma mark Fetch a list of basic events

- (NSMutableString*)basicEventsListSQL:(FWApplication*)application prefs:(FWPreferences*)prefs fromDate:(NSDate*)startDate toDate:(NSDate*)endDate
{
    // If PI not found then do nothing
    PIPlugin* plugin = (PIPlugin*)application.fwPlugin;

    NSMutableString* sql = [NSMutableString stringWithCapacity:2000];
    [sql appendFormat:
     @"select 2, E.Z_PK, E.ZTITLE,"
     " case when E.Z_ENT=%@ then E.ZSTARTDATEINTERNAL else E.ZOCCURRENCEDATEINTERNAL end %@, E.ZSTARTDATETIMEZONE,"
     " E.ZENDDATEINTERNAL, E.ZENDDATETIMEZONE, E.ZLOCATION, E.ZISALLDAY, E.ZBUSYSTATUSINTEGER, E.ZICON, E.ZHASRECURRENCEINTERNAL, C.ZTITLE, C.ZCOLOR, C.ZICON, C.ZTIMEZONE, E.ZROOTITEM,"
     " (select count(*) from ZPICALATTENDEE A where A.ZEVENT=E.ZROOTITEM and A.Z_ENT=%@),"
     " E.Z_OPT + C.Z_OPT,"
     " M.Z_37TAGS %@"
     " from ZPICALCALENDARITEM E"
     " left join ZPICALCALENDAR C on C.Z_PK=E.ZCALENDAR"
     " left join Z_15TAGS M on M.Z_15CALENDARITEMS1=E.Z_PK"
     " where (E.Z_ENT=%@ or E.Z_ENT=%@ or E.Z_ENT=%@)"
     " and (E.ZISDELETEDEXCEPTION is null or E.ZISDELETEDEXCEPTION=0)",
     [plugin.dbEntityKeys objectForKey:kPIEntityTypeEvent],
     kFieldNameEventStartDate,
     [plugin.dbEntityKeys objectForKey:kPIEntityTypeAttendee],
     kFieldNameEventTagID,
     [plugin.dbEntityKeys objectForKey:kPIEntityTypeEvent],
     [plugin.dbEntityKeys objectForKey:kPIEntityTypeEventOccurance],
     [plugin.dbEntityKeys objectForKey:kPIEntityTypeEventException]];
    
    return sql;
}


// ******************************************************************************************
#pragma mark -
#pragma mark Add alarm data to PI events

- (NSString*)addAlarmDataToEventListSQL:(FWApplication*)app keys:(NSString*)keys prefs:(FWPreferences*)prefs
{
    // If PI not found then do nothing
    PIPlugin* plugin = (PIPlugin*)app.fwPlugin;

    NSString* alarmsSql = [NSString stringWithFormat:
                           @"select 2, M.Z_15CALENDARITEMS, A.ZRELATIVETRIGGER, A.ZABSOLUTETRIGGERDATE,"
                           " case when E.Z_ENT=%@ then E.ZOCCURRENCEDATEINTERNAL else E.ZSTARTDATEINTERNAL end,"
                           " E.ZISALLDAY"
                           " from ZPICALALARM A"
                           " join Z_15ALARMS M on M.Z_8ALARMS=A.Z_PK"
                           " join ZPICALCALENDARITEM E on E.Z_PK=M.Z_15CALENDARITEMS"
                           " where M.Z_15CALENDARITEMS in (%@)",
                           [plugin.dbEntityKeys objectForKey:kPIEntityTypeEventOccurance],
                           keys];
        
    return alarmsSql;
}


- (NSString*)augmentedBasicEventSQL:(PIEvent*)event application:(FWApplication*)app
{
    PIPlugin* plugin = (PIPlugin*)app.fwPlugin;

    NSString* augmentSql = [NSString stringWithFormat:
                            @"select 2, 0, E.ZSENSITIVITY, E.ZBUSYSTATUSINTEGER, group_concat(M.Z_37TAGS, ','),"
                            " N.ZNOTES,"
                            " R.ZDAYSOFTHEMONTH, R.ZDAYSOFTHEWEEK, R.ZENDDATE, R.ZFIRSTDAYOFTHEWEEK, R.ZMONTHSOFTHEYEAR, R.ZOCCURRENCECOUNT, R.ZRECURRENCEINTERVAL, R.ZRECURRENCETYPEINTEGER, null, null, null, null"
                            " from ZPICALCALENDARITEM E"
                            " left join Z_15TAGS M on M.Z_15CALENDARITEMS1=E.Z_PK"
                            " left join ZPICALRECURRENCERULE R on R.ZPARENTITEM=E.ZROOTITEM"
                            " left join ZNOTESCONTAINER N on N.ZCALENDARITEM=E.Z_PK"
                            " where E.Z_PK=%@"
                            " group by E.Z_PK",
                            event.key];
    
    return augmentSql;
}

- (NSString*)attendeesSQL:(PIEvent*)event application:(FWApplication*)app
{
    PIPlugin* plugin = (PIPlugin*)app.fwPlugin;
    
    NSString* attendeesSql = [NSString stringWithFormat:@"select 2, ZCOMMONNAME, ZEMAIL, ZSTATUS"
                              " from ZPICALATTENDEE"
                              " where ZEVENT=%@ AND Z_ENT=%@"
                              " order by ZCOMMONNAME",
                              event.key,
                              [plugin.dbEntityKeys objectForKey:kPIEntityTypeAttendee]];
    
    return attendeesSql;
}

@end
